var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hiUrl = require("url/hi");
var http = require("helper/http");
var userUtil = require("helper/user");
var helperOpen = require("helper/open");


//andriod返回键
page.on("back", function() {
    app.closePage();
});

var listdata = mm("do_ListData");
var listView = ui("listView");
listView.bindItems(listdata);

var noData = [{"template":1,"labelContent":"您是老用户，没有新手任务喔"}];

var datas4test = [{"taskName":"社区服务","taskStep":"3/5","taskGrids":[{"labelTitle":"首次加入社区","labelVisible":true,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次签到","labelVisible":true,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次加入圈子","labelVisible":false,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次发布邻里动态","labelVisible":false,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次发布圈子动态","labelVisible":true,"labelText":"获得：30荣誉值+50圈币"}]},
             {"taskName":"圈子任务","taskStep":"2/4","taskGrids":[{"labelTitle":"首次参与圈子活动","labelVisible":true,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次点赞他人动态","labelVisible":true,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次评论他人动态","labelVisible":false,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次添加好友","labelVisible":false,"labelText":"获得：30荣誉值+50圈币"}]},
             {"taskName":"圈子任务","taskStep":"2/3","taskGrids":[{"labelTitle":"首次收藏店铺","labelVisible":true,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次将商品加入购物车","labelVisible":true,"labelText":"获得：30荣誉值+50圈币"},{"labelTitle":"首次加入社区","labelVisible":false,"labelText":"获得：30荣誉值+50圈币"}]}]
var addHaveView = ui("haveLayout").add("haveBorder","source://view/honor/index/module.ui");
function laodPage() {
	if(userUtil.loginToken()==""){
		ui("do_Label_8").text = 0;
		ui("do_Label_6").text = 0;
		ui("do_Label_13").width = 0;
		ui("do_LinearLayout_1").redraw();
		ui("haveLayout").visible = false;
		ui("do_Label_14").text = "0/0";
		listdata.removeAll();
		listdata.addData(noData);
		listView.refreshItems();
		return;
	}
	http.post(hiUrl.url.userTask.myTask,{},function(data){
		var taskCount = 0;//总任务数量
		var finishTaskCount = 0;//完成的任务数量
		ui("do_ImageView_3").source = data.userLoginVo.logo;
		
		var honorLevelName = data.honorLevelName;
		ui("do_Label_11").text = honorLevelName;
		var finishValueString = data.finishValueString;
		ui(addHaveView + ".haveView").items={"centerView":{"label01":"完成全部新手任务可获得：","label02":finishValueString}};
		var totalQuanValue = data.totalQuanValue;
		ui("do_Label_8").text = totalQuanValue;
		var totalHonorValue = data.totalHonorValue;
		ui("do_Label_6").text = totalHonorValue;
		var taskGroup = data.taskGroup;
		var taskMap = data.taskMap;
		
		listdata.removeAll();
		var dataJsion = [];
		for (var i = 0; i < taskGroup.length; i++) {
			var groupTypeCode = taskGroup[i].code;
			var groupTypeName = taskGroup[i].desc;
			var taskList= taskMap[groupTypeCode];
			if(taskList && taskList.length>0){
				var taskGrids = [];
				var finishCount = 0;
				for(var j =0; j<taskList.length;j++){
					if(taskList[j].isFinish=='1'){
						finishCount ++;
						finishTaskCount ++;
						taskGrids.push({"labelTitle":taskList[j].ruleName,"labelVisible":false,"labelText":"已完成"});
					}else{
						if(taskList[j].quanValue==0){
							taskGrids.push({"labelTitle":taskList[j].ruleName,"labelVisible":true,"labelText":"获得："+taskList[j].honorValue+"荣誉值"});
						}else{
							taskGrids.push({"labelTitle":taskList[j].ruleName,"labelVisible":true,"labelText":"获得："+taskList[j].honorValue+"荣誉值+"+taskList[j].quanValue+"圈币"});
						}
						
					}
				}
				taskCount += taskList.length;
				var taskStep = finishCount+"/"+taskList.length;
				dataJsion.push({"taskName":groupTypeName,taskStep:taskStep,"taskGrids":taskGrids});
			}
		}
		
		ui("do_Label_14").text = finishTaskCount+"/"+taskCount;
		if(taskCount>0){
			ui("do_Label_13").width = 480 * finishTaskCount/taskCount;
			ui("do_LinearLayout_1").redraw();
		}else{
			ui("do_Label_13").width = 0;
			ui("do_LinearLayout_1").redraw();
		}
		if(dataJsion.length>0){
			listdata.addData(dataJsion);
			listView.refreshItems();
		}else{
			listdata.addData(noData);
			listView.refreshItems();
		}
		
	});
	
	
}

page.on("new_task_tab_touch", function(index) {
	laodPage();
});

//下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		listView.rebound();
	}
});
// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		listView.rebound();
	}
});

ui("do_ALayout_6").on("touch", "", 300, function(data, e) {
	helperOpen.open("source://view/honor/result/list.ui");
});
ui("do_ALayout_7").on("touch",function(data) {
	helperOpen.open("source://view/user/money/quanList.ui");
});